home *** CD-ROM | disk | FTP | other *** search
- Path: mxsld2.pd.infn.it!LORETI
- From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
- Newsgroups: comp.lang.c
- Subject: Re: how to get weeknumber according calendar
- Date: Wed, 17 Apr 1996 11:36:02 GMT
- Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
- Message-ID: <4l2hhi$1a5k@serra.unipi.it>
- References: <4jt3jr$j58@hdxx05.telecom.ptt.nl>,<4kavnk$aqe@blue.usps.gov>
- Reply-To: loreti@mxsld2.pd.infn.it
- NNTP-Posting-Host: mxsld2.pd.infn.it
-
- In article <4kavnk$aqe@blue.usps.gov>, Bill Seymour <wseymour@email.usps.gov> writes:
- >a.broers@ptt-telecom.unisource.nl (Andre Broers) wrote:
- >>Hello all,
- >>
- >>Hopw can I get a weeknumber following the calendar. This means the
- >>first days in januari can be week 52, 53 or 1 (and not 0).
- >>
- >>Is there an algoritm to calculate this??
- >
- >int weeknumber(struct tm *ptm)
- >{
- > return ptm->tm_yday / 7 + 1;
- >}
-
- Obviously you did not care to read the question, did you? when it
- refers to the first days in January.
-
- Also, weeks start in sundays or mondays; how do you take care of that
- in your code?
-
- Here is something I wrote a couple years ago; hope that helps...
- However the first days in January are week 1; you can modify the code,
- it is in the public domain :-)
- ---------------------------------Begin file: week.c
- #include <stdio.h>
- #include <stdlib.h>
-
- #define isleap(year) (((year) % 4 == 0) && \
- (((year) % 100 != 0) || ((year) % 400 == 0)))
-
- int week(int, int, int);
- int weekday(int, int, int);
-
- int main()
- {
- int d, m, y;
-
- for (;;) {
- printf("Enter day month year ... ");
- scanf("%d %d %d", &d, &m, &y); /* No error checking */
- if (d < 1) break;
- printf("%02d-%02d-%04d: week # %d\n", d, m, y, week(d, m, y));
- }
- exit(EXIT_SUCCESS);
- }
-
- int week(
- int day, /* Returns the week in the year (1..52); day */
- int month, /* month and year are assumed valid (no error */
- int year /* checking). Gregorian calendar assumed. */
- ){ /* A week starts Sunday and ends in Saturday. */
- int firstday; /* Day of the week for Jan. 1, <year> */
- int yearday = day; /* Day of the year for given date */
- static int monthdays[] = {
- 0, 31, 0, 31, /* Dummy, Jan., Feb., Mar. */
- 30, 31, 30, 31, /* Apr., May, Jun., Jul. */
- 31, 30, 31, 30 /* Aug., Sep., Oct., Nov. */
- };
-
- firstday = weekday(1, 1, year);
- monthdays[2] = isleap(year) ? 29 : 28;
- while (--month) {
- yearday += monthdays[month];
- }
- printf("Yearday: %d\n", yearday);
- return ((yearday + firstday - 1) / 7 + 1);
- }
-
- int weekday( /* Returns weekday: 0=Sun, 1=Mon, ..., 6=Sat */
- int day, /* Day of the month (1..31 or whatever), */
- int month, /* month (1..12) and year; assumed valid (no */
- int year /* check done). Gregorian calendar assumed. */
- ){
- int century, result;
-
- if ((month -= 2) < 1) {
- month +=12;
- --year;
- }
- year -= (century = year / 100) * 100;
- result = day + (13*month-1)/5 -2*century + year + year/4 + century/4;
- if (result < 0) result = -result;
- return (result % 7);
- }
- ---------------------------------End file: week.c
- --
- Maurizio Loreti http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
- Un. of Padova, Dept. of Physics - Padova, Italy loreti@padova.infn.it
-